<?php
/**
 * Cookie 组件
 * @author Tongle Xu <xutongle@gmail.com>
 * @copyright Copyright (c) 2003-2103 Jinan TintSoft development co., LTD
 * @license http://www.tintsoft.com/html/about/copyright/
 * @version $Id$
 */
class Cookie {
	
	/**
	 * 前缀
	 * 
	 * @var String
	 */
	public $prefix = '';
	
	/**
	 * 盐
	 * 
	 * @var string Magic salt to add to the cookie
	 */
	public $salt = NULL;
	
	/**
	 * 有效期
	 * 
	 * @var integer Number of seconds before the cookie expires
	 */
	public $expiration = 0;
	
	/**
	 * 作用路径
	 * 
	 * @var string Restrict the path that the cookie is available to
	 */
	public $path = '/';
	
	/**
	 * 作用域
	 * 
	 * @var string Restrict the domain that the cookie is available to
	 */
	public $domain = NULL;
	
	/**
	 * 是否安全连接
	 * 
	 * @var boolean Only transmit cookies over secure connections
	 */
	public $secure = FALSE;
	
	/**
	 *
	 * @var boolean Only transmit cookies over HTTP, disabling Javascript access
	 */
	public $httponly = FALSE;
	
	/**
	 * 组件初始化
	 */
	public function init() {
		$this->secure = (isset ( $_SERVER ['HTTPS'] ) && $_SERVER ['HTTPS'] == 'on') ? true : false;
	}
	
	/**
	 * 判断Cookie是否存在
	 *
	 * @param string $key 变量名
	 * @return boolean 成功则返回true，否则返回 false
	 */
	public function is_set($key) {
		return isset ( $_COOKIE [$this->prefix . $key] );
	}
	
	/**
	 * 设置一个Cookie
	 *
	 * @param string $key Cookie名称
	 * @param string $value Cookie值
	 * @param integer $expiration 有效期
	 * @return boolean
	 */
	public function set($key, $value, $expiration = NULL) {
		if (headers_sent ()) return false;
		if ($expiration === NULL) {
			$expiration = $this->expiration;
		}
		if ($expiration !== 0) {
			$expiration += time ();
		}
		$value = $this->salt ( $this->prefix . $key, $value ) . '~' . $value;
		return setcookie ( $this->prefix . $key, base64_encode ( $value ), $expiration, $this->path, $this->domain, $this->secure, $this->httponly );
	}
	
	/**
	 * 获取Cookie值
	 *
	 * @param string $key cookie name
	 * @param mixed $default default value to return
	 * @return string
	 */
	public function get($key, $default = NULL) {
		if (! isset ( $_COOKIE [$this->prefix . $key] )) {
			return $default;
		}
		$cookie = base64_decode ( $_COOKIE [$this->prefix . $key] );
		$split = strlen ( $this->salt ( $this->prefix . $key, NULL ) );
		if (isset ( $cookie [$split] ) and $cookie [$split] === '~') {
			list ( $hash, $value ) = explode ( '~', $cookie, 2 );
			if ($this->salt ( $this->prefix . $key, $value ) === $hash) {
				return $value;
			}
			$this->delete ( $key );
		}
		return $default;
	}
	
	/**
	 * 删除Cookie
	 *
	 * @param string $name Cookie名称
	 * @return boolean
	 */
	public function delete($key) {
		unset ( $_COOKIE [$this->prefix . $key] );
		return setcookie ( $key, NULL, - 86400, $this->path, $this->domain, $this->secure, $this->httponly );
	}
	
	/**
	 * 加盐
	 *
	 * @param string $name Cookie名称
	 * @param string $value Cookie值
	 * @return string
	 */
	public function salt($name, $value) {
		if (! $this->salt) {
			throw new Base_Exception ( 'A valid cookie salt is required. Please set Cookie::$salt in your bootstrap.php. For more information check the documentation' );
		}
		$agent = isset ( $_SERVER ['HTTP_USER_AGENT'] ) ? strtolower ( $_SERVER ['HTTP_USER_AGENT'] ) : 'unknown';
		return sha1 ( $agent . $name . $value . $this->salt );
	}
}
